package com.teddy.view.tree;

import java.beans.Statement;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Tree;

import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.impl.StatementImpl;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.teddy.controller.RDFSchemaControl;
import com.teddy.model.abstracts.SemanticObject;
import com.teddy.model.skos.DC;
import com.teddy.model.skos.SKOS;
import com.teddy.util.FileManager;

public class ModelTreeItem extends SemanticTreeItem{
	private SemanticObject core; 
	public boolean firstLoad = true;
	
	public ModelTreeItem(Tree parent, int style, SemanticObject so){
		super(parent, style);
		setCore(so);
		try {
			core.loadModel(false);
		} catch (IOException e) {
			e.printStackTrace();
		}
		setText(getCore().getName());
		setItemCount(1);
		if(core.isThesaurus)
			this.setImage(FileManager.getImage(RDFSchemaControl.getResourceString("icon.thesaurus")));
		else
			this.setImage(FileManager.getImage(RDFSchemaControl.getResourceString("icon.taxonomy")));
	}

	public void setCore(SemanticObject core) {
		this.core = core;
	}

	public SemanticObject getCore() {
		return core;
	}
	
	public void expand() throws IOException{
		this.removeAll();
		if (core.isThesaurus){
		setSelectedLanguage(core.getSelectedLanguage());
		ResIterator it = core.getModel().listSubjects();
			while(it.hasNext()){
				Resource r = it.next();
				if (r.getProperty(SKOS.inScheme) != null && r.getProperty(SKOS.broader) == null){
					 ThesaurusTreeITem item = new ThesaurusTreeITem(this, SWT.NONE, r, getSelectedLanguage());
				}
			}
		}
		else{
			ResIterator it = core.getModel().listSubjects();
			while(it.hasNext()){
				Resource r = it.next();
				if (r.getProperty(RDFS.subClassOf) == null){
					 TaxonomyTreeItem item = new TaxonomyTreeItem(this, SWT.NONE, r);
				}
			}
		}
	}
	
	public void refresh() throws IOException{
		firstLoad = true;
		expand();
		//getParent().redraw();
	}

	@Override
	public void remove() {
		core.delete();
		dispose();
		//getParent().redraw();
		// TODO Auto-generated method stub
		
	}

	@Override
	public void rename() {
		// TODO Auto-generated method stub
	}
	
	public void addSubject(String subj){
		core.getDcModel().addProperty(DC.subject, subj, false);
	}
	
	public void addLanguage(String lang){
		core.getDcModel().addProperty(DC.subject, lang, false);
	}
	
	public void removeLanguage(String lang){
		core.getDcModel().removeProperty(DC.language, lang);
	}
	
	public void removeSubject(String subj){
		core.getDcModel().removeProperty(DC.subject, subj);
	}
	
	public Resource addNewConcept(String uri){
		Resource s = core.getModel().createResource(uri);
		ResIterator it = core.getModel().listSubjects();
		//Statement stat = new StatementImpl(s, SKO, )
		
		s.addProperty(SKOS.prefLabel, "New Concept");
		s.addProperty(SKOS.inScheme, it.next().getProperty(SKOS.inScheme).getResource());
		
		core.getModel().add(s.listProperties());
		try {
			expand();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getParent().redraw();
		return s;
	}
}
